package com.kede.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kede.dto.AjaxMessage;
import com.kede.dto.LayuiTableData;
import com.kede.entity.Talk;
import com.kede.service.TalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 【请填写作者】
 * @since 2020-04-19
 */
@Controller
public class TalkController {


    @Autowired
    TalkService talkService;

    @GetMapping("/talkrecords")
    public String talk(){
        return "talk/talklist";
    }

    @ResponseBody
    @GetMapping("talklist")
    public LayuiTableData list(
            String name,
            String tel,
            @RequestParam(value = "page", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit
    ) {
        Page<Talk> query = new Page<Talk>();
        query.setCurrent(currentPage);
        query.setSize(limit);

        Page<Talk> page = talkService.page(query);
        LayuiTableData data = new LayuiTableData();
        data.setData(page.getRecords());
        data.setCount(page.getTotal());

        return data;
    }
    @ResponseBody
    @PostMapping("/talkdelete")
    public AjaxMessage delete(Integer id){
        AjaxMessage message = new AjaxMessage();
        if(talkService.removeById(id)){
            message.setCode(0);
            message.setMsg("删除成功");
        }else {
            message.setCode(1);
            message.setMsg("删除失败");
        }
        return message;
    }
    @GetMapping("/talkdetail")
    public String detail(Integer id, Model model){
        Talk talk = talkService.getById(id);
        model.addAttribute("talk", talk);
        return "talk/talkdetail";
    }
}
